#include "..\StdAfx.h"
#include "ScriptClassTemplate.h"
#include "..\\Manager\\LuaManager.h"

ScriptClassTemplate::ScriptClassTemplate()
{
}

void ScriptClassTemplate::Begin(const char* name)
{
	strcpy_s(szClassName, name);
	m_vLuaFunReg.clear();
	m_mLuaValueReg.clear();
}

const char* ScriptClassTemplate::GetClassName()
{
	return szClassName;
}

int ScriptClassTemplate::index_Setobj(lua_State* L)
{
	ScriptClassTemplate* pT = *(ScriptClassTemplate**)lua_topointer(L, 1);
	const char* strName = lua_tostring(L, 2);
	map<string, ScriptValue>::iterator it = pT->m_mLuaValueReg.find(strName);
	if (it == pT->m_mLuaValueReg.end())
		return 0;
	
	int type = lua_type(L, 3);
	if (type == LUA_TSTRING)
		it->second = lua_tostring(L, 3);
	else if (type == LUA_TNUMBER)
		it->second = (DWORD)lua_tonumber(L, 3);
	return 0;
}

int ScriptClassTemplate::index_Getobj(lua_State* L)
{
	ScriptClassTemplate* pT = *(ScriptClassTemplate**)lua_topointer(L, 1);
	const char* strName = lua_tostring(L, 2);
	map<string, ScriptValue>::iterator it = pT->m_mLuaValueReg.find(strName);
	if (it == pT->m_mLuaValueReg.end())
		return 0;

	if (it->second.GetType() == ScriptValue::VT_DWORD)
		lua_pushnumber(L, it->second.GetNumber());
	else if (it->second.GetType() == ScriptValue::VT_STRING)
		lua_pushstring(L, it->second.c_str());
	else
		return 0;

	return 1;
}

